package core;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import main.SYRRES;

public class FileUtils {	
	
    public static ArrayList<String> load(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        ArrayList<String> lines = new ArrayList<String>();
        try {
            String line = br.readLine();

            while (line != null) {
                lines.add(line);
                line = br.readLine();
            }	        
        } finally {
            br.close();	        
        }	
        return lines;
    }
    
    public static void loadContent(ArrayList<Content> allContent) {
        try {
            ArrayList<String> miniContent = load("data/mini.index_words");
            for (int i = 0; i < miniContent.size(); i++) {
                String[] line = miniContent.get(i).split(" ");
                allContent.add(new Content(line[0], Integer.parseInt(line[1]), Integer.parseInt(line[2])));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * Charger les relations revue - biere et revue - utilisateur dans le fichier mini.relation
     * @param review_user : HashMap qui contient le nom d'utilisateur pour chaque revue
     * @param review_beer : HashMap qui contient le nom de biere pour chaque revue
     */
    public static void loadRelation(HashMap<String, String> review_user, HashMap<String, String> review_beer) {
    	 try {
             ArrayList<String> miniRelation = load("data/mini.relation");
             for (int i = 0; i < miniRelation.size(); i++) {
                 String[] line = miniRelation.get(i).split(" ");
                 if (line[2].equals("review_to_user")) {
                	 review_user.put(line[0], line[1]);
                 } else if (line[2].equals("review_to_beer")) {
                	 review_beer.put(line[0], line[1]);
                 }
             }
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
    }
    
    public static void loadReviewNote(HashMap<String, List<Integer>> review_note) {
        //////////////////////////////////////////////////////////////////////////////////////
        //on remplit la table review_note
        try {
            ArrayList<String> miniOutput = load("data/mini.output");
            for (int i = 0; i < miniOutput.size(); i++) {
                String[] line = miniOutput.get(i).split(" ");
                if (line[1].equals("beer_notes")) {
                	List<Integer> listeNote = new ArrayList<Integer>();
                	for (int j = 0; j < SYRRES.NB_NOTE; j++) {
                		listeNote.add(Integer.parseInt(line[2 + j].substring(2)));
                	}
                	review_note.put(line[0], listeNote);                	         
                } 
            }
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
    
    public static void loadReview(HashMap<String, List<Integer>> review_note, 
    							  ArrayList<Review> allRevue,
    							  HashMap<String, String> review_user, 
    							  HashMap<String, String> review_beer) {
        // Charger les revues de l'utilisateur
        try {
            ArrayList<String> miniInput = load("data/mini.input");
            
            for(int i = 0 ; i < miniInput.size() ; i++) {
                String[] line = miniInput.get(i).split(" ");
                if (line[1].equals("review")) {
                    // creer le contenue du revue
                    HashMap<Integer, Double> content = new HashMap<Integer, Double>();

                    for (int j = 2 ; j < line.length ; j++ ) {
                        String[] wordFrequencies = line[j].split(":");

                        try {
                            content.put(Integer.parseInt(wordFrequencies[0]), Double.parseDouble(wordFrequencies[1]));
                        } catch (NumberFormatException nfe) {
                            System.out.println("line " + i) ;
                        }
                    }

                    if (review_user.get(line[0]) != null && review_note.get(line[0]) != null) {
                    	allRevue.add(new Review(line[0], 
        						review_beer.get(line[0]), 
        						review_user.get(line[0]), 
        						content, 
        						review_note.get(line[0])));
                    }                     
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void writeFile(SetUser allUser, int[][] sim) throws IOException {
    	BufferedWriter bw = new BufferedWriter(new FileWriter("Sim.out"));
    	for (int i = 0; i < sim.length; i++) 
    	for (int j = i + 1; j < sim[i].length; j++) {
    		if (sim[i][j] != 0) {
    			bw.write(allUser.getUser(i).getId() + " " + allUser.getUser(j) + " " + sim[i][j] + "\n");
    		}
    	}
    	bw.close();
    }
}
